package cn.diancan.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.ShopClerk;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.ShopClerkService;
import cn.diancan.util.DateUtil;
import cn.diancan.vo.other.Pager;

@Component("shopClerkService")
public class ShopClerkServiceImpl implements ShopClerkService {
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<ShopClerk> shopClerkDao;

    @Override
    public ShopClerk saveShopClerk(String jobNumber, String clerkName, String remark) throws OutMsgException, Exception {
	ShopClerk shopClerk= new ShopClerk();
	
	if(null!=findByJobNumber(jobNumber)){
	    throw new OutMsgException("已存在该工号");
	}
	
	shopClerk.setClerkName(clerkName);
	shopClerk.setJobNumber(jobNumber);
	
	
	String currentTime =DateUtil.currentDateTime();
	shopClerk.setCreateTime(currentTime);
	shopClerk.setEntryTime(currentTime);
	shopClerk.setIsDelete(CommonType.Delete.NO);
	shopClerk.setRemark(remark);
	shopClerk.setLastUpdateTimeStamp(System.currentTimeMillis());
	shopClerkDao.save(shopClerk);
	
	return shopClerk;
    }
    
    
    

    @Override
    public ShopClerk findByJobNumber(String jobNumber) throws OutMsgException,Exception {
	if(null==jobNumber || "".equals(jobNumber)){
	    throw new OutMsgException("参数错误");
	}
	jobNumber= jobNumber.trim();
	ShopClerk shopClerk =shopClerkDao.get("FROM ShopClerk WHERE jobNumber=?", new Object[]{jobNumber});
	
	return shopClerk;
    }




    @Override
    public List<ShopClerk> findList()throws OutMsgException,Exception {
	List<ShopClerk> list = shopClerkDao.find("FROM ShopClerk WHERE isDelete=?", new Object[]{CommonType.Delete.NO});
	
	return list;
    }

    @Override
    public Pager findByPager(Pager pager,String jobNumber,String  clerkName) throws OutMsgException,Exception{
	String hql = "FROM ShopClerk WHERE isDelete=? ";
	List<Object> param =new ArrayList<Object>();
	param.add(CommonType.Delete.NO);
	
	if(null != jobNumber && !"".equals(jobNumber.trim()) ){
	    hql+=" AND instr(jobNumber,?)>0 " ;
	    param.add(jobNumber);
	}
	if(null != clerkName && !"".equals(clerkName.trim()) ){
	    hql+=" AND instr(clerkName,?)>0 " ;
	    param.add(clerkName);
	}
	
	
	hql+=" ORDER BY id desc";
	
	List<ShopClerk> list = shopClerkDao.find(hql, param,pager.getPageNumber(),pager.getPageSize());
	
	Long count = shopClerkDao.count("SELECT count(id) "+hql, param);
	pager.setList(list);
	pager.setTotalCount(count.intValue());
	
	return  pager;
    }

    @Override
    public ShopClerk findByShopClerkId(Long shopClerkId)  throws OutMsgException, Exception {
	if(null==shopClerkId){
	    throw new OutMsgException("参数错误");
	}
	return shopClerkDao.get("FROM ShopClerk WHERE isDelete=? AND id=?", new Object[]{CommonType.Delete.NO,shopClerkId});
    }

    @Override
    public boolean updateShopClerk(Long shopClerkId, String jobNumber,String clerkName, String remark) throws OutMsgException, Exception {
	ShopClerk shopClerk= findByShopClerkId( shopClerkId) ;
	if(null ==shopClerk){
	    throw new OutMsgException("该信息不存在或已删除");
	}
	shopClerk.setClerkName(clerkName);
	shopClerk.setJobNumber(jobNumber);
	shopClerk.setRemark(remark);	
	shopClerk.setLastUpdateTimeStamp(System.currentTimeMillis());
	shopClerkDao.update(shopClerk);
	return true;
    }

    @Override
    public boolean deleteShopClerk(Long shopClerkId)throws OutMsgException,Exception {
	ShopClerk shopClerk= findByShopClerkId( shopClerkId) ;
	if(null ==shopClerk){
	    throw new OutMsgException("该信息不存在或已删除");
	}
	shopClerk.setIsDelete(CommonType.Delete.YES);
	shopClerk.setLastUpdateTimeStamp(System.currentTimeMillis());
	shopClerkDao.update(shopClerk);
	return true;
    }

    @Override
    public List<ShopClerk> findShopClerk(Long lastUpdateTimeStamp,   Integer pageNo, Integer row)throws OutMsgException,Exception {
	String hql ="FROM ShopClerk WHERE isDelete=? ";
	List<Object> param =new ArrayList<Object>();
	param.add(CommonType.Delete.NO);
	if(null!=lastUpdateTimeStamp){
	    hql+=" AND lastUpdateTimeStamp>=?";
	    param.add(lastUpdateTimeStamp);
	}
	
	return shopClerkDao.find(hql, param, pageNo, row);
    }
    
    
    
    
    
    
    
    
    

}
